/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MyLib;

import Object.Torrent;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author QuangHau
 */
public class MyFile {

    public static void write(byte[] DataByteArray, String DestinationFileName) {
        try {
            OutputStream output = null;
            try {
                output = new BufferedOutputStream(new FileOutputStream(DestinationFileName));
                output.write(DataByteArray);
            } finally {
                output.close();
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, ex);
        } catch (IOException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, ex);
        }
    }

    public static String generateBufferedHash(byte[] buffer) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(buffer);
            byte[] md5 = md.digest();
            BigInteger bi = new BigInteger(1, md5);
            return bi.toString(16);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Torrent.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

//    public static void mergeParts(Torrent torrentFile, String DESTINATION_PATH) {
//        File[] file = new File[torrentFile.getChunks().size()];
//        byte AllFilesContent[] = null;
//
//        int TOTAL_SIZE = 0;
//        int FILE_NUMBER = torrentFile.getChunks().size();
//        int FILE_LENGTH = 0;
//        int CURRENT_LENGTH = 0;
//
//        for (int i = 0; i < FILE_NUMBER; i++) {
//            String path = "Data\\" + torrentFile.getId() + "\\" + torrentFile.getChunks().get(i).getId() + "j.chunk";
//            file[i] = new File(path);
//            TOTAL_SIZE += file[i].length();
//        }
//
//        try {
//            AllFilesContent = new byte[TOTAL_SIZE]; // Length of All Files, Total Size
//            InputStream inStream = null;
//
//            for (int j = 0; j < FILE_NUMBER; j++) {
//                inStream = new BufferedInputStream(new FileInputStream(file[j]));
//                FILE_LENGTH = (int) file[j].length();
//                inStream.read(AllFilesContent, CURRENT_LENGTH, FILE_LENGTH);
//                CURRENT_LENGTH += FILE_LENGTH;
//                inStream.close();
//            }
//
//        } catch (FileNotFoundException e) {
//            System.out.println("File not found " + e);
//        } catch (IOException ioe) {
//            System.out.println("Exception while reading the file " + ioe);
//        } finally {
//            write(AllFilesContent, DESTINATION_PATH);
//        }
//
//        System.out.println("Merge was executed successfully.!");
//    }

    public static void mergeParts(Torrent torrentFile, String DESTINATION_PATH) {
        File[] file = new File[torrentFile.getChunks().size()];
        int FILE_NUMBER = torrentFile.getChunks().size();
        int FILE_LENGTH = 0;
        try {
            OutputStream output = null;
            try {
                output = new BufferedOutputStream(new FileOutputStream(DESTINATION_PATH));
                for (int i = 0; i < FILE_NUMBER; i++) {
                    String path = "Data\\" + torrentFile.getId() + "\\" + torrentFile.getChunks().get(i).getId() + ".chunk";
                    file[i] = new File(path);
                }
                try {
                    InputStream inStream = null;
                    byte[] arrByte = null;
                    for (int j = 0; j < FILE_NUMBER; j++) {
                        inStream = new BufferedInputStream(new FileInputStream(file[j]));
                        FILE_LENGTH = (int) file[j].length();
                        arrByte = new byte[FILE_LENGTH];
                        inStream.read(arrByte);
                        inStream.close();
                        output.write(arrByte);
                    }
                } catch (FileNotFoundException e) {
                    System.out.println("File not found " + e);
                } catch (IOException ioe) {
                    System.out.println("Exception while reading the file " + ioe);
                }

            } finally {
                output.close();
            }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        System.out.println("Merge was executed successfully.!");
    }
}
